<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/2/27
 * Time: 14:19
 */

function ceshi(){
    $a = '测试全局公共函数';
    return $a;
}

function p($arr=[]){
    echo "<pre>";
    print_r($arr);
    die;
}

function p1($arr=[]){
    echo "<p>";
    print_r($arr);
    echo "</p>";
}

/**
 * Notes: 获取反向代理的真是ip地址
 * @return mixed|string
 */
//function get_client_ip() {
//    $ip=false;
//    if(!empty($_SERVER["HTTP_CLIENT_IP"])){
//        $ip = $_SERVER["HTTP_CLIENT_IP"];
//    }
//    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
//        $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
//        if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
//        for ($i = 0; $i < count($ips); $i++) {
//            if (!eregi ("^(10│150.158│192.168|43.136).", $ips[$i])) {
//                $ip = $ips[$i];
//                break;
//            }
//        }
//    }
//    return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
//}
function get_client_ip()
{
    if (getenv('HTTP_CLIENT_IP')) {
        $ip = getenv('HTTP_CLIENT_IP');
    }
    if (getenv('HTTP_X_REAL_IP')) {
        $ip = getenv('HTTP_X_REAL_IP');
    } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
        $ip = getenv('HTTP_X_FORWARDED_FOR');
        $ips = explode(',', $ip);
        $ip = $ips[0];
    } elseif (getenv('REMOTE_ADDR')) {
        $ip = getenv('REMOTE_ADDR');
    } else {
        $ip = '0.0.0.0';
    }

    return $ip;
}




function rj($options=[]){
    $options['status_code'] = $options['status_code']??0;
    $options['code'] = $options['status_code']===0?200:$options['status_code'];
    if(empty($options['msg'])) $options['msg'] = \App\Utils\ApiStatusDict::getCodeMsg($options['status_code']);
//    $options['is_encrypt']  = .env('APP_RM_ENC',0); //数据是否加密
    $options['res_time'] = $options['response_time']??date('Y-m-d H:i:s');
    $options['data'] = $options['data']??null;
    return response()->json($options);
}
/**
 * response error
 * @param $msg
 * @param $err_code
 * @param array|null $data
 * @return \Illuminate\Http\JsonResponse
 */
function re($err_code,$data=null,$msg=null){
    return rj([
        'status_code' => $err_code,
        'data' => $data,
        'msg' => $msg
    ]);
}

/**
 * 数据多层级
 * @param array $data
 * @param int $pid
 * @return array
 */
function subTree($data,  $pid = 0) {
    // 返回的结果
    $arr = [];
    foreach ($data as $val) {
        // 给定的PID是当前记录的上级ID
        if ($pid == $val['pid']) {
            $val['value'] = $val['id'];
            // 递归
            $val['children'] = subTree($data,$val['id']);
            if(empty($val['children'])){
                unset($val['children']);
            }
            $arr[] = $val;
        }
    }
    return $arr;
}

function array_sort($arr,$keys,$type='desc'){
    $keysvalue = $new_array = array();
    foreach ($arr as $k=>$v){
        $keysvalue[$k] = $v[$keys];
    }
    if($type == 'asc'){
        asort($keysvalue);
    }else{
        arsort($keysvalue);
    }
    reset($keysvalue);
    foreach ($keysvalue as $k=>$v){
        $new_array[$k] = $arr[$k];
    }
    return $new_array;
}

//php获取中文字符拼音首字母
function getFirstCharter($str){
    if(empty($str)){return '';}
    $fchar=ord($str);
    if($fchar>=ord('A')&&$fchar<=ord('z')) return strtoupper($str[0]);
    $s1=iconv('UTF-8','gb2312',$str);
    $s2=iconv('gb2312','UTF-8',$s1);
    $s=$s2==$str?$s1:$str;
    $asc=ord($s[0])*256+ord($s[1])-65536;
    if($asc>=-20319&&$asc<=-20284) return 'A';
    if($asc>=-20283&&$asc<=-19776) return 'B';
    if($asc>=-19775&&$asc<=-19219) return 'C';
    if($asc>=-19218&&$asc<=-18711) return 'D';
    if($asc>=-18710&&$asc<=-18527) return 'E';
    if($asc>=-18526&&$asc<=-18240) return 'F';
    if($asc>=-18239&&$asc<=-17923) return 'G';
    if($asc>=-17922&&$asc<=-17418) return 'H';
    if($asc>=-17417&&$asc<=-16475) return 'J';
    if($asc>=-16474&&$asc<=-16213) return 'K';
    if($asc>=-16212&&$asc<=-15641) return 'L';
    if($asc>=-15640&&$asc<=-15166) return 'M';
    if($asc>=-15165&&$asc<=-14923) return 'N';
    if($asc>=-14922&&$asc<=-14915) return 'O';
    if($asc>=-14914&&$asc<=-14631) return 'P';
    if($asc>=-14630&&$asc<=-14150) return 'Q';
    if($asc>=-14149&&$asc<=-14091) return 'R';
    if($asc>=-14090&&$asc<=-13319) return 'S';
    if($asc>=-13318&&$asc<=-12839) return 'T';
    if($asc>=-12838&&$asc<=-12557) return 'W';
    if($asc>=-12556&&$asc<=-11848) return 'X';
    if($asc>=-11847&&$asc<=-11056) return 'Y';
    if($asc>=-11055&&$asc<=-10247) return 'Z';
    return null;
}

function clearSpecialCharacter($str){
    $res = '';
    preg_match_all("/[\x{4e00}-\x{9fa5}a-zA-Z0-9]/u","$str",$result);
    foreach($result[0] as $k=>$v){
        $res .= getFirstCharter($v);
    }
    return $res;
}

function mb_unserialize($str) {
    return preg_replace_callback('#s:(\d+):"(.*?)";#s',function($match){return 's:'.strlen($match[2]).':"'.$match[2].'";';},$str);
}

/**
 * 二维数组根据某个字段排序
 * @param array $array 要排序的数组
 * @param string $keys   要排序的键字段
 * @param string $sort  排序类型  SORT_ASC     SORT_DESC
 * @return array 排序后的数组
 */
function arraySort($array, $keys, $sort = SORT_DESC) {
    $keysValue = [];
    foreach ($array as $k => $v) {
        $keysValue[$k] = $v[$keys];
    }
    array_multisort($keysValue, $sort, $array);
    return $array;
}
